#ifndef _AGENDA_SERVICE_CPP
#define _AGENDA_SERVICE_CPP

#include "AgendaService.h"
#include <string>
#include <list>
using std::string;
using std::list;


AgendaService::AgendaService() {

	// initialize the instances of UserManage and MeetingManage ..
	userManage_ = UserManage::getInstance();
	meetingManage_ = MeetingManage::getInstance();
}


User* AgendaService::userLogIn( string userName, string password ) {
	// Log in the agenda system, return the logged user pointer

	User* usr;


	// find the user by user name ..
	usr = userManage_->findUserByName(userName);
	if(usr!=NULL) {
		// user found, check the password ..
		if(usr->getPassword()==password) {
			// successfully log in, return the user pointer ..
			return usr;
		}
	}

	// fail, return NULL ..
	return NULL;
}


bool AgendaService::userRegister( string userName, string password, string email, string phone ) {
	// Register a new user

	// create the user ..
	return userManage_->createUser(userName, password, email, phone);
}


bool AgendaService::deleteUser( User user ) {
	// Delete a logged user

	User* usr;


	// find the user in the management ..
	usr = userManage_->findUserByName(user.getName());
	if(usr==NULL) return false;

	// if logged, check if any meeting remains ..
	if(MeetingManage::getInstance()->listAllMeetings(usr->getName()).size()>0) {
		// there are meetings remaining, return false ..
		return false;
	}

	// delete the user from the management ..
	userManage_->deleteUser(user);

	// successful, return true ..
	return true;
}


list<User> AgendaService::listAllUsers( ) {
	// List all the users under management

	// return the list of all users ..
	return userManage_->listAllUsers();
}


list<Meeting> AgendaService::listAllMeetings( string userName ) {
	// List all the meetings of a user

	// return the list of all meetings ..
	return meetingManage_->listAllMeetings(userName);
}


list<Meeting> AgendaService::listAllSponsorMeetings( string userName ) {
	// List all the sponsor meetings of a user

	// return the list of meetings ..
	return meetingManage_->listAllSponsorMeetings(userName);
}


list<Meeting> AgendaService::listAllParticipateMeetings( string userName ) {
	// List all the participator meetings of a user

	// return the list of meetings ..
	return meetingManage_->listAllParticipateMeetings(userName);
}


bool AgendaService::createMeeting( string userName, string title, string participator, Date stime, Date etime) {
	// Create a new meeting

	// create the meeting ..
	return meetingManage_->addMeeting(userName, participator, stime, etime, title);
}


Meeting* AgendaService::meetingQuery( string title ) {
	// Query a meeting by its title

	// return the meeting ..
	return meetingManage_->queryMeetingByTitle(title);
}


list<Meeting> AgendaService::meetingQuery( string userName, Date stime, Date etime ) {
	// Query meetings by time period of a user

	// return the list of the meetings ..
	return meetingManage_->queryMeetingsByTime(userName, stime, etime);
}


bool AgendaService::deleteMeeting( string userName, string title ) {
	// Delete a meeting of a user

	// delete the meeting ..
	return meetingManage_->deleteMeeting(userName, title);
}


bool AgendaService::deleteAllMeetings( string userName ) {
	// Delete all the meetings of a user

	// delete the meetings ..
	return meetingManage_->deleteAllMeetings(userName);
}


void AgendaService::startAgenda() {
	// Load the data from the file path
	userManage_->initUsersList(fileManage_->getInstance()->readUsersFromFile());
	meetingManage_->initMeetingsList(fileManage_->getInstance()->readMeetingsFromFile());
}


void AgendaService::quitAgenda() {
	// Save the data to the file path
	fileManage_->writeUsersToFile(userManage_->getInstance()->acquireUsersList());
	fileManage_->writeMeetingsToFile(meetingManage_->getInstance()->acquireMeetingsList());
}


#endif // !_AGENDA_SERVICE_CPP
